package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysRegionInfo;
import com.emep.changzhi.analyse.repository.basics.SysRegionInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import com.emep.changzhi.analyse.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 流域业务层
 * @date 2018/2/24 17:03
 * @version V1.0
 */
@Service
public class SysRegionInfoService {
    @Resource
    private SysRegionInfoRepository regionInfoRepository;
    private static final Logger logger = LoggerFactory.getLogger(SysRegionInfoService.class);

    /**
     *
     * 新增流域
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysRegionInfo save(Map<String, Object> payload) {
        SysRegionInfo regionInfo = null;
        try {
            SysRegionInfo regionInfo1 = (SysRegionInfo) MapUtils.convertMap(SysRegionInfo.class,payload);
            regionInfo=regionInfoRepository.save(regionInfo1);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return regionInfo;
    }

    /**
     *
     * 根据id 删除流域
     *
     * @param id
     */
    public boolean deleteRegion(String id) {
        boolean result = false;
        try {
            Integer result1 = regionInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }
    /**
     *查询所有流域
     *
     * @return
     */
    public List<SysRegionInfo> findAllRegions() {
        List<SysRegionInfo> AreaInfos = null;
        try {
            AreaInfos =  regionInfoRepository.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return AreaInfos;
    }

    /**
     *
     * 根据id查询流域信息
     *
     * @param id
     * @return
     */
    public SysRegionInfo findRegionById(String id){
        SysRegionInfo regionInfo = null;
        try {
            regionInfo = regionInfoRepository.findRegionById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return regionInfo;
    }

    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysRegionInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return regionInfoRepository.findAll(pageable);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }
    /**
     *
     * @param page 页数
     * @param size 数量
     * @param regionInfo 参数
     * @return
     */
    public Page<SysRegionInfo> findBookCriteria(Integer page, Integer size, final SysRegionInfo regionInfo) {
        //规格定义
        Specification<SysRegionInfo> specification = new Specification<SysRegionInfo>() {
            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<SysRegionInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>(); //所有的断言
                if(!StringUtils.isEmpty(regionInfo.getRegionName())){
                    Predicate likeNickName = cb.equal(root.get("keyTypeName").as(String.class),regionInfo.getRegionName());
                    predicates.add(likeNickName);
                }

                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        //分页信息
        Pageable pageable = new PageRequest(page-1,size); //页码：前端从1开始，jpa从0开始，做个转换
        //查询
        return regionInfoRepository.findAll(specification,pageable);
    }
}
